package com.inossem.warehousestorage_inossem.activity.inventory;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inventory.InventoryOverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryIntegrationBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.InventoryManageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/12 0012.
 */

public class InventoryOverViewActivity extends BaseActivity {

    private TextView title;
    private PullableListView list;
    private InventoryOverviewAdapter adapter;

    private TextView nextBtn;

    private InventoryAreaBean bean;

    private String inventoryVoucher;
    private String voucherType;
    private String factoryCode;
    private String inventoryAddressCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        SPUtils.removeInformation(this, SPUtils.KEY_INVENTORY_INFORMATION);
        SPUtils.removeInventoryIntegrationInformation(InventoryOverViewActivity.this);
        setContentView(R.layout.activity_inventory_overview);
        initView(this);
        initData(this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextBtn = (TextView) activity.findViewById(R.id.nextBtn);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_inventory_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        list = (PullableListView) activity.findViewById(R.id.list);
        list.setPullDownFlag(false);
        list.setPullUpFlag(false);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_inventory_overview_title));
        inventoryVoucher = activity.getIntent().getStringExtra(InventoryExtra.INVENTORY_VOUCHER);
        voucherType = activity.getIntent().getStringExtra(InventoryExtra.VOUCHER_TYPE);
        factoryCode = activity.getIntent().getStringExtra(InventoryExtra.FACTORY_CODE);
        inventoryAddressCode = activity.getIntent().getStringExtra(InventoryExtra.INVENTORY_ADDRESS_CODE);

//        getData(activity);

        nextBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                if (InventoryUtils.getTotalCheckCount(bean) <= 0) {
//                    Utils.middleToast(activity, "盘点数不能为空");
//                    return;
//                } else {
                Intent intent = new Intent(activity, InventoryCommitActivity.class);
                intent.putExtra(InventoryExtra.INVENTORY_VOUCHER, inventoryVoucher);
                activity.startActivity(intent);
//                }
            }
        });

        setmScanListener((String scanResult, int length) -> {
            String position = QRCodeUtils.DecryptQRCode(scanResult);
//            String an = position.substring(0, position.lastIndexOf("-"));
//            an = an.replaceFirst("-", " ");
//            adapter.doSeach(position);
            InventoryIntegrationBean.InventoryListBean inventoryBean = getInventoryIntegrationBean(activity, position);
            if (inventoryBean == null) {
                adapter.doSeach(position);
            } else {
                Intent intent = new Intent(activity, InventoryItemActivity.class);
                intent.putExtra(InventoryExtra.AREA_NUMBER_VALUE, inventoryBean.getAreaNumberValue());
                intent.putExtra(InventoryExtra.POSITION_NUMBER_VALUE, inventoryBean.getPositionNumberValue());
                activity.startActivity(intent);
            }
        }, QRCodeConstant.TYPE_POSITION);

    }

    public PullableListView getList() {
        return list;
    }

    public void setList(PullableListView list) {
        this.list = list;
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    protected void onResumeImpl() {
        getData(this);
    }

    private void getData(Activity activity) {

        InventoryManageRequest.overview(activity, inventoryVoucher, new GsonBeanListener<InventoryAreaBean>() {
            @Override
            public void onSuccess(InventoryAreaBean bean) {
                bean.setFactoryCode(factoryCode);
                bean.setInventoryAddressCode(inventoryAddressCode);
                bean.setInventoryVoucher(inventoryVoucher);
                bean.setVoucherType(voucherType);
                if (adapter == null) {
                    adapter = new InventoryOverviewAdapter(InventoryOverViewActivity.this, bean);
                    getList().setAdapter(adapter);
                } else {
                    adapter.setBean(bean);
                    adapter.notifyDataSetChanged();
                }
                InventoryOverViewActivity.this.bean = bean;
                SPUtils.saveInformation(activity, SPUtils.KEY_INVENTORY_INFORMATION, InventoryOverViewActivity.this.bean);
                if (TextUtils.isEmpty(SPUtils.getInventoryIntegrationInformation(activity))) {
                    String json = getInventoryIntegrationInformation(bean.getBody(), inventoryVoucher);
                    SPUtils.saveInventoryIntegrationInformation(activity, json);
                }
            }
        });
    }

    private String getInventoryIntegrationInformation(List<InventoryAreaBean.AreaListBean> areaList, String inventoryVoucher) {
        InventoryIntegrationBean inventoryIntegrationBean = new InventoryIntegrationBean();
        List<InventoryIntegrationBean.InventoryListBean> inventoryList = new ArrayList<>();
        for (int i = 0; i < areaList.size(); i++) {
            InventoryAreaBean.AreaListBean areaItem = areaList.get(i);
            String areaNumber = areaItem.getPrefix();
            String areaNumberValue = areaItem.getArea_code();
            List<InventoryAreaBean.AreaListBean.PositionListBean> positionList = areaItem.getPosition_list();
            for (int j = 0; j < positionList.size(); j++) {
                InventoryAreaBean.AreaListBean.PositionListBean positionItem = positionList.get(j);
                String positionNumber = positionItem.getSuffix();
                String positionNumberValue = positionItem.getPosition_code();
                InventoryIntegrationBean.InventoryListBean inventoryItem = new InventoryIntegrationBean.InventoryListBean();
                inventoryItem.setInventoryVoucher(inventoryVoucher);
                inventoryItem.setAreaNumber(areaNumber);
                inventoryItem.setAreaNumberValue(areaNumberValue);
                inventoryItem.setPositionNumber(positionNumber);
                inventoryItem.setPositionNumberValue(positionNumberValue);
                inventoryItem.setFullPosition(areaNumberValue + "-" + positionNumberValue);
                inventoryList.add(inventoryItem);
            }
        }
        inventoryIntegrationBean.setInventoryList(inventoryList);
        Gson gson = new Gson();
        return gson.toJson(inventoryIntegrationBean, InventoryIntegrationBean.class);
    }

    private InventoryIntegrationBean.InventoryListBean getInventoryIntegrationBean(Context context, String position) {
        Gson gson = new Gson();
        List<InventoryIntegrationBean.InventoryListBean> inventoryList = gson.fromJson(SPUtils.getInventoryIntegrationInformation(context), InventoryIntegrationBean.class).getInventoryList();
        if (inventoryList != null && !inventoryList.isEmpty()) {
            for (int i = 0; i < inventoryList.size(); i++) {
                InventoryIntegrationBean.InventoryListBean inventoryItem = inventoryList.get(i);
                if (inventoryItem.getFullPosition().equals(position)) {
                    return inventoryItem;
                }
            }
        }
        return null;
    }

}
